<template>
  <j-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="型材编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-search-select-tag
                placeholder="请选择型材"
                v-decorator="['profileCode', { rules: [{ required: true, message: '请选择型材!' }] }]"
                dict="wms_material,material_code,id"
                @change="handleProfileChange"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="型材名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input placeholder="选择型材后自动带出" disabled v-decorator="['profileName']" />
            </a-form-item>
          </a-col>

          <!-- 长风软件类型特有字段 -->
          <template v-if="softwareType === '0'">
            <a-col :span="12">
              <a-form-item label="颜色" :labelCol="labelCol" :wrapperCol="wrapperCol">
                <a-input placeholder="请输入颜色" v-decorator="['color']" />
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item label="宽度(mm)" :labelCol="labelCol" :wrapperCol="wrapperCol">
                <a-input-number style="width: 100%" placeholder="请输入宽度" v-decorator="['width']" />
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item label="高度(mm)" :labelCol="labelCol" :wrapperCol="wrapperCol">
                <a-input-number style="width: 100%" placeholder="请输入高度" v-decorator="['height']" />
              </a-form-item>
            </a-col>
          </template>

          <a-col :span="12">
            <a-form-item label="长度(mm)" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number
                style="width: 100%"
                placeholder="请输入长度"
                v-decorator="['length', { rules: [{ required: true, message: '请输入长度!' }] }]"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="数量(根)" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number
                style="width: 100%"
                placeholder="请输入数量"
                :min="1"
                v-decorator="['quantity', { rules: [{ required: true, message: '请输入数量!' }] }]"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="重量(kg)" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number style="width: 100%" placeholder="请输入重量" v-decorator="['weight']" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="备注" :labelCol="{ span: 3 }" :wrapperCol="{ span: 21 }">
              <a-textarea placeholder="请输入备注" v-decorator="['remark']" :rows="2" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
import { FormMixin } from '@/mixins/formMixin'
import JSearchSelectTag from '@/components/dict/JSearchSelectTag'
import { getAction } from '@/api/manage'

// 使用require方式导入uuid
const uuidv4 = require('uuid').v4

export default {
  name: 'ProfileFormModal',
  mixins: [FormMixin],
  components: {
    JSearchSelectTag,
  },
  data() {
    return {
      form: this.$form.createForm(this),
      title: '添加型材明细',
      visible: false,
      model: {},
      labelCol: { span: 6 },
      wrapperCol: { span: 18 },
      confirmLoading: false,
      softwareType: '0', // 默认长风
      editIndex: undefined, // 编辑时的索引
      url: {
        getMaterialInfo: '/wms/material/getById',
      },
    }
  },
  methods: {
    // 添加型材明细
    add(softwareType) {
      this.edit({}, softwareType)
    },

    // 编辑型材明细
    edit(record, softwareType, index) {
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      this.softwareType = softwareType
      this.editIndex = index

      // 设置标题
      this.title = index !== undefined ? '编辑型材明细' : '添加型材明细'

      // 如果是编辑，设置表单值
      if (this.model.id) {
        this.$nextTick(() => {
          this.form.setFieldsValue(this.model)
        })
      }
    },

    // 关闭弹窗
    close() {
      this.visible = false
      this.editIndex = undefined
    },

    // 提交表单
    handleOk() {
      const that = this

      // 表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true

          // 构建提交数据
          let formData = Object.assign(this.model, values)

          // 确保有ID
          if (!formData.id) {
            formData.id = uuidv4()
          }

          // 设置领料状态为未领料
          formData.status = 0

          // 发送数据到父组件
          this.$emit('ok', formData, this.editIndex)

          that.confirmLoading = false
          that.close()
        }
      })
    },

    // 选择型材后的回调
    handleProfileChange(value) {
      if (value) {
        // 获取型材信息
        getAction(`${this.url.getMaterialInfo}?id=${value}`).then((res) => {
          if (res.success && res.result) {
            // 设置型材名称和其他信息
            this.form.setFieldsValue({
              profileName: res.result.materialName,
              color: res.result.color || '',
              width: res.result.width || undefined,
              height: res.result.height || undefined,
              weight: res.result.weight || undefined,
            })
          }
        })
      } else {
        this.form.setFieldsValue({
          profileName: '',
          color: '',
          width: undefined,
          height: undefined,
          weight: undefined,
        })
      }
    },
  },
}
</script>

<style scoped>
</style> 